package com.guanzi.big_data_mq_process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guanzi.big_data_mq_process.dao.OrderDetailsDao;
import com.guanzi.big_data_mq_process.model.dto.OrderDetailsDTO;
import com.guanzi.big_data_mq_process.model.entity.OrderDetails;
import com.guanzi.big_data_mq_process.model.query.OrderDetailsQuery;
import com.guanzi.big_data_mq_process.model.vo.OrderDetailsVO;
import com.guanzi.big_data_mq_process.service.IOrderDetailsService;
import com.guanzi.big_data_mq_process.util.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (OrderDetails) 服务实现类
 *
 * @author xuguang
 * @since 2025-05-06 21:57:44
 */
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsDao, OrderDetails> implements IOrderDetailsService {
    /**
     * 分页列表
     */
    @Override
    public PageResult<OrderDetailsVO> getPage(OrderDetailsQuery query) {
        Page<OrderDetails> page = new Page<>(query.getPageNum(), query.getPageSize());
        QueryWrapper<OrderDetails> queryWrapper = new QueryWrapper<>();

        // 排序
        //SortDTO sortDto = query.getSortDto();
        // if (null != sortDto) {
        //     if (sortDto.getColumnName().equals("xxxTime")) {
        //         sortDto.setColumnName(ColumnUtils.toUnderscoreCase(sortDto.getColumnName()));
        //     }
        // }

        return new PageResult(this.baseMapper.getPage(page, queryWrapper, query));
    }


    /**
     * 详情
     */
    @Override
    public OrderDetailsVO getInfo(String id) {
        QueryWrapper<OrderDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("order_detail_id", id);
        return this.baseMapper.getInfo(queryWrapper);
    }

    /**
     * 保存
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrderDetails(OrderDetailsDTO dto) {
        OrderDetails orderDetails = null;
        orderDetails = this.getById(dto.getOrderDetailId());
        if (null == orderDetails) {
            // 新增
            orderDetails = new OrderDetails();
        }
        BeanUtils.copyProperties(dto, orderDetails, "order_detail_id");
        return this.saveOrUpdate(orderDetails);
    }

    /**
     * 删除数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByIdList(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            throw new RuntimeException("待删除的数据不能为空!");
        }
        UpdateWrapper<OrderDetails> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                // TODO 如果有逻辑删除字段,以及主键ID
                .set("delete_mark", 1)
                .in("order_detail_id", idList);
        return update(updateWrapper);
    }

}
